/**
 * 
 */
package messenger;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author (Group 42) William Orem, Joseph Hamilton, Kevin Benik
 * 
 */
public class UserInterface {
	private UserAccount user;
	private Scanner sc = new Scanner(System.in);
	private CommChannel client;

	/**
	 * 
	 */
	public UserInterface() {

	}

	/**
	 * @param i
	 * 		index of contact to show
	 */
	private void showAContact(int i) {
		System.out.println(user.getContacts().get(i));

	}

	/**
	 * @return
	 * 		builds and returns a contact
	 */
	public Contact buildContact() {
		sc.useDelimiter(System.getProperty("line.separator"));
		System.out.println("enter username");
		String username = sc.next();
		System.out.println("enter pid");
		String pid = sc.next();
		System.out.println("enter first name");
		String first = sc.next();
		System.out.println("enter last name");
		String last = sc.next();
		System.out.println("enter phone");
		String phone = sc.next();
		System.out.println("enter email");
		String email = sc.next();
		System.out.println("enter grad year");
		int year = sc.nextInt();
		sc.reset();
		return new Contact(pid, first, last, phone, email, year, username);
	}

	/**
	 *  builds a message to send
	 */
	public void createMessage() {
		sc.useDelimiter(System.getProperty("line.separator"));
		System.out.println("to: ");
		String to = sc.next();
		System.out.println("subject: ");
		String subject = sc.next();
		System.out.println("message: ");
		String message = sc.next();
		client.setOutMessage(new Message(to, user.userName, subject, message));
		// TODO log message
		sc.reset();
	}

	/**
	 * @param i
	 * 		index of user to delete
	 */
	public void deleteContact(int i) {

		user.getContacts().remove(i);

	}
	


	/**
	 * @param st
	 * 			an array of strings to display
	 */
	public void displayContacts(ArrayList<String> st) {
		for (int i = 0; i < st.size(); ++i) {
			System.out.println(st.get(i));
		}
		System.out.println("");
	}

	/**
	 * displays the menu for the user
	 */
	public void displayMenu() {
		if (user != null && user.isLoggedIn()) {
			System.out.print(Commands.CCONTACT.ordinal() + ") Find contact  ");
			System.out.print(Commands.SCONTACT.ordinal() + ") List contacts  ");
			System.out
					.print(Commands.DCONTACT.ordinal() + ") Delete contact  ");
			System.out.print(Commands.MCONTACT.ordinal() + ") Mark contact  ");
			System.out.println(Commands.CONTACT.ordinal() + ") Show contact ");

			System.out.print(Commands.CMESSAGE.ordinal() + ") enter message  ");
			System.out
					.print(Commands.SMESSAGE.ordinal() + ") show messages   ");

			System.out.println(Commands.EXIT.ordinal() + ") exit\n");

		} else {
			System.out.println(Commands.LOGIN.ordinal() + ") login");
			System.out.println("0) exit\n");
		}

	}

	/**
	 *  displays the messages sent to user
	 */
	public void displayMessages() {
		for (Message temp : user.getTmpMessages()) {
			System.out.println(temp.getDate().getTime());
			System.out.println("\tfrom: " + temp.getFrom() + "\tsubject: "
					+ temp.getSubject());
			System.out.println("\t" + temp.getBody());
		}
		user.getTmpMessages().clear();
		System.out.println("");
	}


	/**
	 *   displays the user info
	 */
	public void displayUser() {
		System.out.println("pid: " + user.pId);
		System.out.println("first name: " + user.firstName);
		System.out.println("last name: " + user.lastName);
		System.out.println("phone number: " + user.phone);
		System.out.println("email: " + user.eMail);
		System.out.println("grad year: " + user.year);

	}

	/**
	 * @return
	 * 		the command
	 */
	public int getCommand() {
		System.out.print(Commands.MENU.ordinal() + ") show Menu ");
		System.out.println("Command: ");
		int command = sc.nextInt();
		return command;

	}

	/**
	 * @return the user
	 */
	public UserAccount getUser() {
		return user;
	}

	/**
	 * @return
	 */
	public boolean login() {
		boolean addaccount = false;
		sc.useDelimiter(System.getProperty("line.separator"));
		System.out.println("enter username");
		String username = sc.next();
		username = username.trim();
		if ((new File(username + ".data")).exists()) {
			System.out.println("enter password");
			String password = sc.next();
			user = new UserAccount(username, password);
		} else {
			setupUser(username);
			addaccount = true;
		}
		sc.reset();
		if (user.isLoggedIn()) {
			client = new CommChannel(ChatManager.serverIP, 4444, user);
			client.start();
			if (addaccount)
				client.setCommand(CommCommand.ADDCCONTACT);
			return true;
		}
		return false;

	}

	/**
	 * @param username
	 */
	public void setupUser(String username) {
		System.out.println("New Account for " + username);
		sc.useDelimiter(System.getProperty("line.separator"));
		System.out.println("Enter your password");
		String password = sc.next();
		user = new UserAccount(username, password);
		System.out.println("Enter your pid");
		user.setpId(sc.next());
		System.out.println("Enter your first name");
		user.setFirstName(sc.next());
		System.out.println("Enter your last name");
		user.setLastName(sc.next());
		System.out.println("Enter your phone number");
		user.setPhone(sc.next());
		System.out.println("Enter your email");
		user.seteMail(sc.next());
		System.out.println("Enter your grad year");
		user.setYear(sc.nextInt());
		sc.reset();
	}

	/**
	 * 
	 */
	public void tearDown() {
		sc.close();
		if (client != null)
			client.setCommand(CommCommand.QUIT);
	}

	/**
	 *  main loop function processes commands
	 */
	public void uiMain() {
		Commands command = Commands.MENU;

		while (command != Commands.EXIT) {

			System.out.println("\n");
			switch (command) {

			case CCONTACT:
				System.out.println("Enter username you wish to find");
				client.setFindUser(sc.next());
				client.setCommand(CommCommand.GETCONTACT);
				break;
			case SCONTACT:
				displayContacts(user.getFriends());
				break;

			case LOGIN:
				if (!login())
					System.out.print("Bad Password!");
				else if (!client.getCommand().equals(CommCommand.ADDCCONTACT)) {
					client.setCommand(CommCommand.LOGIN);
				}
				break;

			case CMESSAGE:
				createMessage();
				client.setCommand(CommCommand.SEND);
				break;

			case DCONTACT:
				displayContacts(user.getFriends());
				System.out.println("DELETE Which user?");
				deleteContact(sc.nextInt());
				break;

			case CONTACT:
				displayContacts(user.getFriends());
				System.out.println("Which user?");
				showAContact(sc.nextInt());
				break;

			case EXIT:
				break;

			case MCONTACT:
				displayContacts(user.getFriends());
				System.out.println("Which user?");
				int u = sc.nextInt();
				displayLists();
				System.out.println("Which list?");
				user.addTOList(user.getContacts().get(u), ListCategory.values()[sc.nextInt()]);
				break;

			case SMESSAGE:
				displayMessages();
				break;

			case MENU:
			default:
				displayMenu();
				break;

			}
			command = command.fromInt(getCommand());
			if (user == null
					&& (command != Commands.EXIT & command != Commands.LOGIN))
				command = Commands.MENU;
		}

		if (user != null && user.isLoggedIn())
			user.saveState();
		tearDown();

	}

	/**
	 *  display the category list
	 */
	private void displayLists() {
		for(ListCategory l : ListCategory.values())
			System.out.println("("+l.ordinal()+") "+l.name());
	}

}
